package com.xingceng.stock.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiceng.stock.entity.StockBusiness;
import com.xiceng.stock.entity.StockMarketIndexInfo;
import com.xiceng.stock.entity.StockMarketLogPrice;
import com.xiceng.stock.vo.InnerMarketVO;
import com.xingceng.stock.mapper.StockMarketIndexInfoMapper;
import com.xingceng.stock.mapper.StockMarketLogPriceMapper;
import com.xingceng.stock.properties.MarketProperties;
import com.xingceng.stock.service.StockBusinessService;
import com.xingceng.stock.mapper.StockBusinessMapper;
import com.xingceng.stock.util.DateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ZYZ
* @description 针对表【stock_business(主营业务表)】的数据库操作Service实现
* @createDate 2025-11-07 14:39:25
*/
@Service
@RequiredArgsConstructor
public class StockBusinessServiceImpl extends ServiceImpl<StockBusinessMapper, StockBusiness>
    implements StockBusinessService{

    private final StockMarketIndexInfoMapper stockMarketIndexInfoMapper;
    private final StockMarketLogPriceMapper stockMarketLogPriceMapper;
    private final MarketProperties marketProperties;

    @Override
    public List<InnerMarketVO> innerIndexAll() {
        // 1. 获取国内大盘指数编码集合
        List<String> ids = marketProperties.getInner();
        // 2. 获取最近的有效的股票交易时间
        LocalDateTime last = DateTimeUtil.getLastDateTime4Stock(LocalDateTime.now());
        // mock data
        String mockDate = "20211226105600";
        last = LocalDateTime.parse(mockDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        // 3. 查询
        // 3.1 根据大盘编码和日期查询 股票大盘数据详情表
        List<StockMarketIndexInfo> marketIndexInfoList = stockMarketIndexInfoMapper.selectByIdsAndDateTime(ids, last);
        // 3.2 根据大盘编码和日期查询 股票大盘 开盘价与前收盘价流水表
        LocalDate localDate = last.toLocalDate();
        List<StockMarketLogPrice> marketLogPriceList = stockMarketLogPriceMapper.selectByIdsAndDateTime(ids, localDate);
        //4. 组装数据
        return marketIndexInfoList.stream().map((marketIndexInfo) -> {
                    InnerMarketVO vo = InnerMarketVO.builder()
                            .code(marketIndexInfo.getMarkId())
                            .name(marketIndexInfo.getMarkName())
                            .curDate(marketIndexInfo.getCurTime().toLocalDate().toString())
                            .tradeAmt(marketIndexInfo.getTradeAccount())
                            .tradeVol(marketIndexInfo.getTradeVolume())
                            .tradePrice(marketIndexInfo.getCurrentPrice())
                            .upDown(marketIndexInfo.getUpdownRate())
                            .build();
                    marketLogPriceList.forEach((marketLogPrice) -> {
                        if (marketLogPrice.getMarketCode().equals(marketIndexInfo.getMarkId())) {
                            vo.setOpenPrice(marketLogPrice.getOpenPrice());
                            vo.setPreClosePrice(marketLogPrice.getPreClosePrice());
                        }else {
                            vo.setOpenPrice(BigDecimal.ZERO);
                            vo.setPreClosePrice(BigDecimal.ZERO);
                        }
                    });
                    return vo;
                }
        ).collect(Collectors.toList());
    }
    
}




